/* Copyright 2018-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
 * applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package com.gitee.gosqlgo.demo;

import static com.github.drinkjava2.jsqlbox.JSQLBOX.gctx;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

import org.apache.commons.lang.StringUtils;

import com.gitee.drinkjava2.gosqlgo.ClassBuilder;
import com.gitee.gosqlgo.demo.Pages.homepage;
import com.gitee.gosqlgo.demo.Pages.page404;
import com.gitee.gosqlgo.demo.Pages.pageError;
import com.github.drinkjava2.jbeanbox.JBEANBOX;
import com.github.drinkjava2.jdialects.StrUtils;
import com.github.drinkjava2.jtransactions.tinytx.TinyTxConnectionManager;
import com.github.drinkjava2.jwebbox.WebBox;

import gosqlgo.Dao;

/**
 * Change /xxx.htm calls to html pages show
 * 
 * @author Yong Zhu
 * @since 1.0.0
 */
@SuppressWarnings("all")
public class Dispatch {

	public static void dispatch(PageContext pageContext) {
		WebBox box = doDispatch(pageContext);
		box.show(pageContext);
	}

	public static WebBox doDispatch(PageContext pageContext) {
		String uri = StringUtils.substringBefore(((HttpServletRequest) pageContext.getRequest()).getRequestURI(), ".");
		uri = StringUtils.substringAfterLast(uri, "/").toLowerCase();
		if (StringUtils.isEmpty(uri))
			return new homepage();
		if ("qry".equals(uri))
			return qryDispatch(pageContext);
		if ("java".equals(uri))
			return javaDispatch(pageContext);
		if ("tx".equals(uri))
			return txDispatch(pageContext);
		try {
			return JBEANBOX.getBean(Class.forName("com.gitee.gosqlgo.demo.Pages$" + uri));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return new page404();
		}
	}

	public static WebBox qryDispatch(PageContext pageContext) {
		String sql = pageContext.getRequest().getParameter("sql");
		if (StrUtils.isEmpty(sql))
			return new WebBox().setText("");
		StringBuilder mtd = new StringBuilder("\r public String qry() { \r");
		mtd.append("Object result=iQueryForObject(").append(sql).append("); \r");
		mtd.append("return \"\"+result; \r");
		mtd.append("} ");
		Class<?> daoClass;
		try {
			daoClass = ClassBuilder.build(Dao.class, null, "com.gosqlgo.demo", ClassBuilder.getRandomClassName(30),
					"import static com.github.drinkjava2.jsqlbox.JSQLBOX.*;\r", mtd.toString());
			Dao d = JBEANBOX.getBean(daoClass);
			return new WebBox().setText(d.qry());
		} catch (Exception e) {
			e.printStackTrace();
			return new WebBox().setText("");
		}
	}

	public static WebBox javaDispatch(PageContext pageContext) {
		String java = pageContext.getRequest().getParameter("java");
		if (StrUtils.isEmpty(java))
			return new WebBox().setText("");
		StringBuilder mtd = new StringBuilder("\r public String java() { \r");
		mtd.append(java);
		mtd.append("} ");
		Class<?> daoClass;
		try {
			daoClass = ClassBuilder.build(Dao.class, null, "com.gosqlgo.demo", ClassBuilder.getRandomClassName(30),
					"import static com.github.drinkjava2.jsqlbox.JSQLBOX.*;\r", mtd.toString());
			Dao d = (Dao) daoClass.newInstance();
			return new WebBox().setText(d.java());
		} catch (Exception e) {
			e.printStackTrace();
			return new WebBox().setText("");
		}
	}

	public static WebBox txDispatch(PageContext pageContext) {
		TinyTxConnectionManager tx = (TinyTxConnectionManager) gctx().getConnectionManager();
		tx.startTransaction(gctx().getDataSource(), Connection.TRANSACTION_READ_COMMITTED);
		try {
			WebBox box = javaDispatch(pageContext);
			tx.commit(gctx().getDataSource());
			return box;
		} catch (SQLException e) {
			tx.rollback(gctx().getDataSource());
			return new pageError();
		}
	}

}
